/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package economicocontrol.dao;

import economicocontrol.Validaciones.ValidacionesCampos;
import economicocontrol.dao.entity.RegistroEntradaSalida;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author CyberOD
 */

    public class RegistroEntradaSalidaDAO extends ManagerDAO{        
        public  List<RegistroEntradaSalida> findAll(){
          try {
            EntityManager em = getEntityManager();
            TypedQuery<RegistroEntradaSalida> query =em.createNamedQuery("RegistroEntradaSalida.findAll", RegistroEntradaSalida.class);
            
            List<RegistroEntradaSalida> results = query.getResultList();
            em.close();
            return  results;
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        return  null;
    } 

    public  List<RegistroEntradaSalida> intevaloFecha(int idPersanal, Date fechaSalida, Date fechaEntrada){
          try {
            EntityManager em = getEntityManager();
            TypedQuery<RegistroEntradaSalida> query =em.createNamedQuery("RegistroEntradaSalida.intevaloFecha", RegistroEntradaSalida.class).setParameter("idPersanal",idPersanal).setParameter("fechaSalida",fechaSalida).setParameter("fechaEntrada",fechaEntrada);
            List<RegistroEntradaSalida> results = query.getResultList();
            em.close();
            return  results;
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        return  null;
    }
    
    public  List<RegistroEntradaSalida> buscarIdEntSalXEco(int idEconomico){
          try {
            EntityManager em = getEntityManager();
            TypedQuery<RegistroEntradaSalida> query =em.createNamedQuery("RegistroEntradaSalida.idEconomico", RegistroEntradaSalida.class).setParameter("ideconomico",idEconomico);
            List<RegistroEntradaSalida> results = query.getResultList();
            em.close();
            return  results;
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        return  null;
    }
    
    public  List<RegistroEntradaSalida> buscarEntSalIdEco(int idEntSal){
          try {
            EntityManager em = getEntityManager();
            TypedQuery<RegistroEntradaSalida> query =em.createNamedQuery("RegistroEntradaSalida.findByIdRegistroKilometraje", RegistroEntradaSalida.class).setParameter("idRegistroKilometraje",idEntSal);
            List<RegistroEntradaSalida> results = query.getResultList();
            em.close();
            return  results;
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        return  null;
    }
    
    public int idMax(){
          try {
            EntityManager em = getEntityManager();
            TypedQuery<RegistroEntradaSalida> query =em.createNamedQuery("RegistroEntradaSalida.idMax", RegistroEntradaSalida.class);
            List<RegistroEntradaSalida> results = query.getResultList();
            Object[] array =results.toArray();
            em.close();
            return  Integer.parseInt(array[0].toString()==null?"0":array[0].toString());
        }catch(Exception ex) {
            //ex.printStackTrace();
            return  0;
        }
        
    }
    
        public int listKmEntrada(int idEconomico){
          try {
            EntityManager em = getEntityManager();
            TypedQuery<RegistroEntradaSalida> query =em.createNamedQuery("RegistroEntradaSalida.listKmEntrada", RegistroEntradaSalida.class).setParameter("idEconomico",idEconomico);
            List<RegistroEntradaSalida> results = query.getResultList();
            Object[] array =results.toArray();
            em.close();
            return  Integer.parseInt(array[0].toString()==null?"0":array[0].toString());
        }catch(Exception ex) {
            //ex.printStackTrace();
            return  0;
        }
        
    }

    public float promKmFechaSalidaEntradaPersonal(int idPersanal, int diaDeSemanaSalida, int diaDeSemanaEntrada, int numDias){
          try {
            EntityManager em = getEntityManager();
            TypedQuery<RegistroEntradaSalida> query =em.createNamedQuery("RegistroEntradaSalida.promKmFechaSalidaEntradaPersonal", RegistroEntradaSalida.class).setParameter("idPersanal",idPersanal).setParameter("diaDeSemanaSalida",diaDeSemanaSalida).setParameter("diaDeSemanaEntrada",diaDeSemanaEntrada).setParameter("numDias",numDias);
            List<RegistroEntradaSalida> results = query.getResultList();
            Object[] array =results.toArray();
            em.close();
            
            return  Float.parseFloat(array[0].toString()==null?"0.0":array[0].toString());
        }catch(Exception ex) {
            //ex.printStackTrace();
            return  0;
        }
        
    }
    
    public float promKmFechaSalidaEntradaEconomico(int idEconomico, int diaDeSemanaSalida, int diaDeSemanaEntrada, int numDias){
          try {
            EntityManager em = getEntityManager();
            TypedQuery<RegistroEntradaSalida> query =em.createNamedQuery("RegistroEntradaSalida.promKmFechaSalidaEntradaEconomico", RegistroEntradaSalida.class).setParameter("idEconomico",idEconomico).setParameter("diaDeSemanaSalida",diaDeSemanaSalida).setParameter("diaDeSemanaEntrada",diaDeSemanaEntrada).setParameter("numDias",numDias);
            List<RegistroEntradaSalida> results = query.getResultList();
            Object[] array =results.toArray();
            em.close();
            return  Float.parseFloat(array[0].toString()==null?"0.0":array[0].toString());
        }catch(Exception ex) {
            //ex.printStackTrace();
            return 0;
        }
        
    }
    
    public void insert( RegistroEntradaSalida depEn){
          try {              
            EntityManager em = getEntityManager();               
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(depEn);
            em.getTransaction().commit();
            em.close();
        }catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void update(RegistroEntradaSalida depEn){
        try {      
            EntityManager em = getEntityManager();               
            em = getEntityManager();
            em.getTransaction().begin();            
            RegistroEntradaSalida dep=em.merge(depEn);
            em.flush();            
            em.getTransaction().commit();
            em.close();
        }catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void delete (RegistroEntradaSalida depEn){
                       
        try {            
           
            EntityManager em = getEntityManager();               
            em = getEntityManager();
            em.getTransaction().begin();            
            RegistroEntradaSalida dep=em.merge(depEn);
            em.remove(dep);
            em.getTransaction().commit();
            em.close();
        }catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
